﻿using PEAnalyzer.ModeView.MainPage;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PEAnalyzer.ModeView
{
    internal class MainPageModeView : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private ObservableCollection<GridViewItemModeView> m_listAnalyzeFiles = new ObservableCollection<GridViewItemModeView>();

        // 过滤参数
        // 0 包含，1 不包含
        bool m_bStrategy;
        string m_strKey = "";

        public MainPageModeView()
        {
            
        }

        public void SetListChangeCallback(NotifyCollectionChangedEventHandler notify)
        {
            m_listAnalyzeFiles.CollectionChanged += notify;
        }

        public void SetFilter(int strategy, string key)
        {
            m_bStrategy = strategy == 0;
            m_strKey = key;

            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("FilesInfo"));
        }

        ObservableCollection<GridViewItemModeView> FileInfoFilter()
        {
            if(m_strKey == "")
            {
                return m_listAnalyzeFiles;
            }

            ObservableCollection<GridViewItemModeView> collect = new ObservableCollection<GridViewItemModeView>();
            foreach (var item in m_listAnalyzeFiles)
            {
                // 文件名
                var fileName = item.PEInfo.Get_FileName();
                if (fileName.IndexOf(m_strKey, StringComparison.CurrentCultureIgnoreCase) >= 0)
                {
                    collect.Add(item);
                    continue;
                }

                // 模块名 + 导入函数
                bool find = false;
                var importFunctions = item.PEInfo.Get_ImportFunctionArray();
                foreach (var importItem in importFunctions)
                {
                    if(importItem.Get_Name().IndexOf(m_strKey, StringComparison.CurrentCultureIgnoreCase) >= 0)
                    {
                        find = true;
                        break;
                    }

                    foreach (var importFunctionItem in importItem.Get_FuncArray())
                    {
                        if(importFunctionItem.Get_Name().IndexOf(m_strKey, StringComparison.CurrentCultureIgnoreCase) >= 0)
                        {
                            find = true;
                            break;
                        }
                    }

                    if(find == true)
                    {
                        break;
                    }
                }

                if(find == true)
                {
                    collect.Add(item);
                    continue;
                }

                // 导出函数
                var exportFunction = item.PEInfo.Get_ExportFunction();
                foreach (var exportItem in exportFunction.Get_FunctionArray())
                {
                    if(exportItem.Get_Name().IndexOf(m_strKey, StringComparison.CurrentCultureIgnoreCase) >= 0)
                    {
                        collect.Add(item);
                        break;
                    }
                }
            }

            return collect;
        }

        public ObservableCollection<GridViewItemModeView> FilesInfo
        {
            get => FileInfoFilter();

            set
            {
                m_listAnalyzeFiles = value;
                this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("FilesInfo"));
            }
        }

        public int PECount
        {
            get => m_listAnalyzeFiles.Count();
            set 
            {
                this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("PECount"));
            }
        }
    }
}
